<template>
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="180px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="姓名">
              <el-input v-model="form.username" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="手机号">
              <el-input v-model="form.userpone" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上级姓名">
              <el-input v-model="form.refusername" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上级手机号">
              <el-input v-model="form.refuserpone" clearable />
            </el-form-item>
          </el-col>
          <!--          <el-col :span="8">-->
          <!--            <el-form-item label="注册日期" width="236">-->
          <!--              <el-date-picker-->
          <!--                v-model="use_time"-->
          <!--                value-format="yyyy-MM-dd HH:mm:ss"-->
          <!--                type="daterange"-->
          <!--                range-separator="-"-->
          <!--                start-placeholder="开始时间"-->
          <!--                end-placeholder="结束时间"-->
          <!--                :default-time="['00:00:00', '23:59:59']"-->
          <!--              />-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="6" :offset="18">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <!--               <el-button type="primary" @click="handleExport">导出</el-button>-->
              <!-- <Exprotcomponent :params="form" url="admin/order/card_ticket_export" type="php"></Exprotcomponent> -->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div class="listDiv" v-loading="tableLoading">
      <el-table ref="tableData" :data="tableData" style="width: 100%" height="500px">
        <!--   -->
        <el-table-column
          fixed
          type="index"
          label="编号"
          width="50"
          align="center"
          :index="table_index"
        />
        <el-table-column label="姓名" prop="nickname" align="center"></el-table-column>
        <el-table-column label="手机号" prop="username" align="center" width="120px" />
        <el-table-column label="上级姓名" prop="project_name" align="center" width="120px">
          <template slot-scope="scope">
            <span v-if="scope.row.is_system === true ">悦拼悦美</span>
            <span v-if="scope.row.get_ref == null && scope.row.is_system === false ">--</span>
            <span
              v-if="scope.row.get_ref !== null&& scope.row.is_system === false "
            >{{scope.row.get_ref.nickname}}</span>
          </template>
        </el-table-column>
        <el-table-column label="上级手机号" align="center" width="120px">
          <template slot-scope="scope">
            <span v-if="scope.row.is_system === true ">{{scope.row.get_ref.username}}</span>
            <span v-if="scope.row.get_ref == null && scope.row.is_system === false ">--</span>
            <span
              v-if="scope.row.get_ref !== null&& scope.row.is_system === false "
            >{{scope.row.get_ref.username}}</span>
          </template>
        </el-table-column>
        <el-table-column label="购买PLUS礼包名称" align="center" width="120px">
          <template slot-scope="scope">
            <span v-if="scope.row.orderdata.length == 0 ">--</span>
            <span v-if="scope.row.orderdata.length != 0 ">{{scope.row.orderdata.good_name}}</span>
          </template>
        </el-table-column>
        <el-table-column label="购买PLUS礼包类型" prop="gift_type_name" align="center" width="120px" />
        <el-table-column label="购买PLUS礼包价格" align="center" width="120px">
          <template slot-scope="scope">
            <span v-if="scope.row.orderdata.length == 0 ">--</span>
            <span v-if="scope.row.orderdata.length != 0 ">{{scope.row.orderdata.pay_amount}}</span>
          </template>
        </el-table-column>
        <el-table-column label="会员注册时间" align="center" width="200px">
          <template slot-scope="scope">
            <span v-if="scope.row.orderdata.length == 0 ">--</span>
            <span v-if="scope.row.orderdata.length != 0 ">{{scope.row.orderdata.pay_time}}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="220">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.is_system === true"
              type="primary"
              size="small"
              @click="rewrite (scope.row)"
            >修改上级</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <el-pagination
          :current-page="form.page"
          :page-sizes="[10,20,50,100]"
          :page-size="form.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page_totals"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      <!-- 修改上级 -->
      <el-dialog title="修改上级" :visible.sync="dialogVisible" width="468px" @close="clear" :close-on-click-modal="false">
        <el-form
          ref="ruleForm"
          :rules="ruleForm"
          :inline="true"
          label-width="150px"
          label-position="right"
        >
          <!--          <el-form-item  label="上级姓名:">-->
          <!--            <el-input v-model="refusername"  />-->
          <!--          </el-form-item>-->
          <el-row>
            <el-col :span="24">
              <el-form-item label="订单类型" prop="orderType">
                <el-radio-group v-model="form1.orderType">
                  <el-radio label="1">项目订单</el-radio>
                  <el-radio label="2">礼包订单</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="订单编号" prop="orderNo">
                <el-input v-model="form1.orderNo" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="分成规则" prop="reset">
                <el-checkbox v-model="form1.reset">是否重新分成</el-checkbox>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="上级手机号:">
            <el-input v-model="form1.refuserpone" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" :disabled="isDisable" @click="update()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  chechUpdateUserRef,
  plusUserList,
  updateUserRef,
} from "../../api/vipIndex";
export default {
  name: "GiftBagOrder",
  data() {
    return {
      use_time: [],
      pay_time: [],
      form: {
        page: 1,
        pageSize: 10,
        username: "", //	用户名搜索
        userpone: "", //	用户手机号搜索
        refusername: "", //	上级姓名
        refuserpone: "", //上级手机号
      }, // 分页
      refusername: "", //修改上级名称
      // refuserpone: "", //修改上级手机号
      form1: {
        refuserpone: "",
        orderType: "",
        orderNo: "",
        reset: "",
      },
      page_totals: 0, // 总条目数,
      tableData: [],
      // 列表loding
      tableLoading: false, //表格加载
      dialogVisible: false, //弹出框状态
      plusUserInfo: [], //会员信息
      ruleForm: {
        refusername: [{ required: true, message: "为必填项", trigger: "blur" }],
        refuserpone: [{ required: true, message: "为必填项", trigger: "blur" }],
      },
      isDisable: false, // 表单重复提交
    };
  },
  // filters: {
  //   nullFn(opt) {
  //     if (opt) {
  //       return opt;
  //     } else {
  //       return "--";
  //     }
  //   },
  //   // 无数据显示
  //   noFn(opt) {
  //     if (opt) {
  //       return opt;
  //     } else {
  //       return "";
  //     }
  //   }
  // },
  created() {
    this.getList(this.form);
  },
  methods: {
    toRoutes(name) {
      let route = { name };
      this.$router.push(route);
    },
    // 序号
    table_index(index) {
      return (this.form.page - 1) * this.form.pageSize + index + 1;
    },
    // 获取列表
    getList(e) {
      plusUserList(e)
        .then((res) => {
          if (res.status == 200) {
            this.tableData = res.data.data.data;
            this.page_totals = res.data.data.total;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning",
            });
          }
          this.tableLoading = false;
        })
        .catch((err) => {
          // this.errorStatus(err.msg);
          this.tableLoading = false;
        });
    },
    //修改上级 -弹窗显示
    rewrite(item) {
      console.log("item", item);
      this.dialogVisible = true;
      this.plusUserInfo = item;
      // this.refuserpone = "";
      this.form1.refuserpone = "";
    },
    //修改上级
    update() {
      chechUpdateUserRef({
        userId: this.plusUserInfo.user_id,
        refusername: this.refusername,
        // refuserpone: this.refuserpone,
        refuserpone: this.form1.refuserpone,
      })
        .then((result) => {
          const res = result.data;
          if (res.code === 200) {
            // 输入
            this.$confirm(
              "确认修改用户【" +
                this.plusUserInfo.nickname +
                "】的上级为" +
                res.data.username +
                "？",
              "提示",
              {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
              }
            )
              .then(() => {
                this.updateUser();
              })
              .catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消修改",
                });
              });
          } else {
            this.$message.error(res.msg);
          }
          this.handleSearch();
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },

    //修改上级
    updateUser() {
      updateUserRef({
        userId: this.plusUserInfo.user_id,
        refusername: this.refusername,
        // refuserpone: this.refuserpone,
        refuserpone: this.form1.refuserpone,
        orderType: this.form1.orderType,
        orderNo: this.form1.orderNo,
        reset: this.form1.reset ? 1 : 0,
      })
        .then((result) => {
          const res = result.data;
          if (res.code === 200) {
            this.$message({
              message: "plus会员修改上级成功",
              type: "success",
            });
          } else {
            this.$message.error(res.msg);
          }
          this.dialogVisible = false;
          this.handleSearch();
        })
        .catch((err) => {
          this.dialogVisible = false;
          this.$message.error(err);
        });
    },
    // 清空弹窗内容
    clear() {
      this.form1 = {
        reset: "",
      };
      this.$refs.ruleForm.resetFields();
    },
    handleSearch() {
      this.form.page = 1;
      this.getList(this.form);
    },

    // 导出
    handleExport() {
      let datas = { ...this.form };
      delete datas.page;
      delete datas.pageSize;
      orderExport(datas)
        .then((res) => {
          commonFn.downFile("导出", res);
        })
        .catch((err) => {
          this.errorStatus(err.msg);
        });
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getList(this.form);
    },

    // 选择页数
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },

    // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status,
      });
    },

    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    },
  },
};
</script>
<style scoped>
.app-container {
  padding: 20px !important;
  background: transparent;
}
/* 主页列表样式 */
.listDiv {
  margin-bottom: 20px;
  padding: 0 20px;
}
.formDiv {
  padding: 20px;
}
.el-range-editor--medium.el-input__inner {
  width: 205px;
}
.pagesCon {
  text-align: right;
  margin-top: 20px;
}
.allCenter {
  text-align: center;
}
.spanToInput {
  width: 205px;
  min-height: 30px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: block;
  border: 1px solid #ddd;
  text-align: left;
  padding: 0 12px;
}
.line {
  height: 25px;
  background: #f7f7f7;
}
.recommend {
  min-width: 280px;
  color: #1d2023;
  text-align: center;
  font-size: 16px;
  margin-left: 25px;
}
.boxContent {
  margin-top: 20px;
  height: 25px;
  line-height: 25px;
  border: 1px solid #ddd;
  text-align: center;
  padding: 0 12px;
}
.arrow {
  text-align: center;
  padding: 12px 0 0 0;
}
.fontColor {
  font-size: 16px;
  text-align: center;
  margin-bottom: 15px;
}
.mb_20 {
  font-size: 20px;
  text-align: center;
  margin-top: 20px;
}
</style>
